package com.xzavier.yunlian_interview.controller;

import com.xzavier.yunlian_interview.entity.User;
import com.xzavier.yunlian_interview.service.UserService;
import com.xzavier.yunlian_interview.service.ex.FileException;
import com.xzavier.yunlian_interview.service.ex.LoginException;
import com.xzavier.yunlian_interview.service.ex.RegisterException;
import com.xzavier.yunlian_interview.utils.JsonResult;
import jakarta.servlet.http.HttpServletRequest;
import org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Base64Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Base64;

@RestController
@RequestMapping("interview/userInfo")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @PostMapping("profile")
    public JsonResult<Boolean> updateUserInfo(HttpServletRequest request, @RequestBody User user) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new RegisterException("token缺失");
        }
        boolean b = userService.updateUserInfo(user);
        return new JsonResult<>(200, b);
    }

    @PostMapping("avatar")
    public JsonResult<Boolean> updateUserAvatar(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws SQLException, IOException {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new RegisterException("token缺失");
        }
        if (file.isEmpty()) {
            throw new FileException("文件缺失");
        }
        if (file.getSize() > 10 * 1024 * 1024) {
            throw new FileException("文件超出限制");
        }
        userService.updateAvatar(userId, file.getBytes());
        return new JsonResult<>(200, true);
    }

    //    @GetMapping("getavatar")
//    public ResponseEntity<byte[]> getUserAvatar(HttpServletRequest request) {
//        String userId = (String) request.getAttribute("userId");
//        if (userId == null) {
//            throw new RegisterException("token缺失");
//        }
//        byte[] avatar = userService.getAvatar(userId);
//        if (avatar == null) {
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.IMAGE_JPEG);
//            return new ResponseEntity<>(avatar, headers, HttpStatus.OK);
//        }
//        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
//    }
    @GetMapping("getavatar")
    public JsonResult<String> getUserAvatar(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new RegisterException("token缺失");
        }
        byte[] avatar = userService.getAvatar(userId);
        if(avatar == null) {
            return new JsonResult<>(200);
        }
        String s = Base64.getEncoder().encodeToString(avatar);
        return new JsonResult<>(200, s);
    }
}
